package com.hp.ssmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hp.ssmp.dao.CartMapper;
import com.hp.ssmp.dao.DetailsMapper;
import com.hp.ssmp.dao.OrderMapper;
import com.hp.ssmp.entity.TCart;
import com.hp.ssmp.entity.TOrder;
import com.hp.ssmp.entity.TOrderDetails;
import com.hp.ssmp.entity.dto.TCartDto;
import com.hp.ssmp.service.CartService;
import com.hp.ssmp.service.DetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service("cartService")
@Transactional
public class CartServiceImpl extends ServiceImpl<CartMapper, TCart> implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DetailsMapper detailsMapper;

    @Override
    public List<TCartDto> getBy(Long userid) {
        QueryWrapper<TCartDto> wrapper = new QueryWrapper<TCartDto>();
        if (userid != null && !"".equals(userid)) {
            wrapper.eq("c.userid", userid);
        }
        List<TCartDto> tCartDtoBy = cartMapper.getTCartDtoBy(wrapper);
        return tCartDtoBy;
    }

    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,readOnly = false)
    public boolean account(Long userid, Long addressid) {
        QueryWrapper<TCartDto> wrapper = new QueryWrapper<TCartDto>();
        wrapper.eq("c.userid",userid);
        List<TCartDto> cartDtoList = cartMapper.getTCartDtoBy(wrapper);
        BigDecimal totalPrice=new BigDecimal(0);
        for (TCartDto cartDto : cartDtoList) {
            BigDecimal goodsprice=cartDto.getGoodsprice();
            BigDecimal num=new BigDecimal(cartDto.getNum());
            totalPrice=totalPrice.add(goodsprice.multiply(num));
        }

        TOrder order = new TOrder();
        order.setUserid(userid);
        order.setOrdertime(new Date());
        order.setAddressid(addressid);
        order.setOrderstatus(1L);
        order.setTotalprice(totalPrice);
        orderMapper.insert(order);

//        生成订单信息

        Long orderid = order.getOrderid();
        for (TCartDto cartDto : cartDtoList) {
            TOrderDetails tOrderDetails = new TOrderDetails();
            tOrderDetails.setGoodsid(cartDto.getGoodsid());
            tOrderDetails.setGoodsname(cartDto.getGoodsname());
            tOrderDetails.setGoodsnum(cartDto.getNum());
            tOrderDetails.setGoodsprice(cartDto.getGoodsprice());
            tOrderDetails.setOrderid(orderid);
            detailsMapper.insert(tOrderDetails);
        }

//        清空购物车
        QueryWrapper<TCart> wrapper1= new QueryWrapper<TCart>();
        wrapper1.eq("userid",userid);
        cartMapper.delete(wrapper1);
        return true;
    }
}
